const MYSQL = require("../../sql/mysql");
const MYSQLConfig = require("../../sql/database");
const SQLName = MYSQLConfig.datasheetName.user_token;
const DayJs = require("dayjs");
/**
 * 处置所有过期的token
 * @brief 本模块将所有过期的token但token在数据库中的state并未及时更新的记录进行处理
 * @note 本模块只是更新记录操作,并不删除过期的token
 * @return {Promise<{code:number,msg:string} | {code:number,msg:string}>}
 */
module.exports = async () => {
  try {
    // 当前时间
    let date_now = DayJs(Date.now()).valueOf();
    // 将过期的token置为  state = 2
    let updateTokenExpired = `
        UPDATE ${SQLName} SET 
        state = 2
        WHERE expired_date < ${date_now} and state = 1
    `;
    await MYSQL.queryPromise(updateTokenExpired);
    return { code: 0, msg: "操作成功" };
  } catch (error) {
    throw { code: 500, msg: "创建token失败,在更新过期token时发生错误" };
  }
};
